function [capacity_multiuser, user_scheduling, fig_multiuser] = multiuser_capacity(EbN0_dB, num_users, scheduling_policies)
% 多用户系统容量分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   scheduling_policies - 调度策略 ('round_robin', 'max_cqi', 'proportional_fair')
% 输出参数：
%   capacity_multiuser - 多用户容量
%   user_scheduling - 用户调度结果
%   fig_multiuser - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
bandwidth = 180e3; % 带宽 (Hz)
num_subcarriers = 64; % 子载波数量
noise_power_dBm = -174 + 10*log10(bandwidth/num_subcarriers);
noise_power = 10^((noise_power_dBm-30)/10);
num_time_slots = 100; % 时间槽数量

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
num_policies = length(scheduling_policies);
capacity_multiuser = zeros(num_snr, num_user_cases, num_policies);
user_scheduling = zeros(num_user_cases, num_time_slots);

fprintf('多用户系统容量分析...\n');

% 多用户容量和调度分析
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx);
        
        % 生成用户信道增益
        user_channels = randn(K, num_subcarriers) + 1i * randn(K, num_subcarriers);
        channel_gains = abs(user_channels).^2;
        
        for policy_idx = 1:num_policies
            policy = scheduling_policies{policy_idx};
            
            % 初始化调度结果
            scheduled_users = zeros(num_time_slots, 1);
            total_capacity = 0;
            
            % 用户平均速率 (用于比例公平调度)
            average_rates = zeros(K, 1);
            
            % 时域调度仿真
            for slot = 1:num_time_slots
                switch lower(policy)
                    case 'round_robin'
                        % 轮询调度
                        scheduled_user = mod(slot-1, K) + 1;
                        
                    case 'max_cqi'
                        % 最大CQI调度
                        [~, scheduled_user] = max(mean(channel_gains, 2));
                        
                    case 'proportional_fair'
                        % 比例公平调度
                        if slot == 1
                            scheduled_user = 1; % 初始选择用户1
                        else
                            % 计算比例公平度量
                            pf_metrics = mean(channel_gains, 2) ./ (average_rates + 1e-6);
                            [~, scheduled_user] = max(pf_metrics);
                        end
                        
                    otherwise
                        % 默认为轮询
                        scheduled_user = mod(slot-1, K) + 1;
                end
                
                scheduled_users(slot) = scheduled_user;
                
                % 计算调度用户的容量
                user_channel = mean(channel_gains(scheduled_user, :));
                user_snr = snr_linear * user_channel / noise_power;
                
                if user_snr > 0
                    slot_capacity = (bandwidth/num_time_slots) * log2(1 + user_snr);
                    total_capacity = total_capacity + slot_capacity;
                end
                
                % 更新平均速率 (比例公平调度)
                if strcmpi(policy, 'proportional_fair')
                    alpha = 0.01; % 平滑因子
                    for k = 1:K
                        if k == scheduled_user
                            average_rates(k) = (1-alpha) * average_rates(k) + alpha * slot_capacity;
                        else
                            average_rates(k) = (1-alpha) * average_rates(k);
                        end
                    end
                end
            end
            
            % 保存容量结果
            capacity_multiuser(snr_idx, user_idx, policy_idx) = total_capacity;
            
            % 保存调度结果 (最后一次迭代)
            if snr_idx == num_snr
                user_scheduling(user_idx, :) = scheduled_users';
            end
        end
    end
end

%% 可视化结果
fig_multiuser = figure('Name', '多用户系统容量分析', 'Position', [100, 100, 1200, 800]);

% 子图1: 多用户容量 vs SNR (不同调度策略)
subplot(2, 2, 1);
user_fixed = min(3, num_user_cases); % 选择中等用户数量
for policy_idx = 1:num_policies
    plot(EbN0_dB, capacity_multiuser(:, user_fixed, policy_idx)/1e6, 'LineWidth', 2, 'Color', colors(policy_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title(sprintf('多用户容量 vs 信噪比 (%d用户)', num_users(user_fixed)));
legend(scheduling_policies, 'Location', 'NorthWest');

% 子图2: 容量 vs 用户数量 (固定SNR)
subplot(2, 2, 2);
snr_fixed = round(num_snr/2);
for policy_idx = 1:num_policies
    plot(num_users, capacity_multiuser(snr_fixed, :, policy_idx)/1e6, 'o-', 'LineWidth', 2, 'Color', colors(policy_idx, :));
    hold on;
end
grid on;
xlabel('用户数量');
ylabel('容量 (Mbps)');
title(sprintf('多用户容量 vs 用户数量 (Eb/N0 = %d dB)', EbN0_dB(snr_fixed)));
legend(scheduling_policies, 'Location', 'NorthWest');

% 子图3: 用户调度公平性
subplot(2, 2, 3);
% 计算调度公平性 (Jain公平性指数)
fairness_index = zeros(num_user_cases, num_policies);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    
    for policy_idx = 1:num_policies
        % 计算每个用户的调度次数
        scheduling_counts = zeros(K, 1);
        for k = 1:K
            scheduling_counts(k) = sum(user_scheduling(user_idx, :) == k);
        end
        
        % Jain公平性指数
        fairness_index(user_idx, policy_idx) = (sum(scheduling_counts)^2) / (K * sum(scheduling_counts.^2));
    end
end

bar(1:num_user_cases, fairness_index, 'FaceColor', colors(1, :));
legend(scheduling_policies, 'Location', 'NorthEast');
xlabel('用户数量');
ylabel('Jain公平性指数');
title('调度策略公平性比较');
set(gca, 'XTick', 1:num_user_cases, 'XTickLabel', num_users);

% 子图4: 多用户分集增益
subplot(2, 2, 4);
% 计算多用户分集增益
multiuser_diversity_gain = zeros(num_user_cases, num_policies);
for user_idx = 1:num_user_cases
    for policy_idx = 1:num_policies
        if user_idx > 1
            % 相对于单用户的增益
            multiuser_diversity_gain(user_idx, policy_idx) = 10*log10(...
                capacity_multiuser(snr_fixed, user_idx, policy_idx) / ...
                capacity_multiuser(snr_fixed, 1, policy_idx));
        else
            multiuser_diversity_gain(user_idx, policy_idx) = 0;
        end
    end
end

plot(num_users, multiuser_diversity_gain(:, 1), 'o-', 'LineWidth', 2, 'Color', colors(1, :));
hold on;
plot(num_users, multiuser_diversity_gain(:, 2), 's-', 'LineWidth', 2, 'Color', colors(2, :));
plot(num_users, multiuser_diversity_gain(:, 3), '^-', 'LineWidth', 2, 'Color', colors(3, :));
grid on;
xlabel('用户数量');
ylabel('多用户分集增益 (dB)');
title('多用户分集增益');
legend(scheduling_policies, 'Location', 'NorthWest');

fprintf('多用户容量分析完成！\n');

end